const mysql = require('mysql2/promise')
const config = require('./config')

async function checkAuthors () {
  try {
    const connection = await mysql.createConnection({
      host: config.database.host,
      port: config.database.port,
      user: config.database.user,
      password: config.database.password,
      database: config.database.name
    })

    // 检查是否有authors表
    console.log('📋 检查authors表是否存在:')
    const [tables] = await connection.execute("SHOW TABLES LIKE 'authors'")
    console.log('authors表存在:', tables.length > 0)

    if (tables.length > 0) {
      // 检查作者表结构
      console.log('\n📋 作者表结构:')
      const [structure] = await connection.execute('DESCRIBE authors')
      console.table(structure)

      // 检查作者数据
      console.log('\n📊 作者数据:')
      const [authors] = await connection.execute(
        'SELECT * FROM authors LIMIT 10'
      )
      console.table(authors)
    }

    // 检查小说表的作者信息
    console.log('\n📚 小说表中的作者信息:')
    const [novels] = await connection.execute(
      'SELECT id, title, author FROM novels LIMIT 10'
    )
    console.table(novels)

    await connection.end()
  } catch (error) {
    console.error('❌ 错误:', error.message)
  }
}

checkAuthors()
